home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / onboard / utils.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  7.4 KB  |  230 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. from xml.dom import minidom
  6. from copy import deepcopy
  7. from xml.dom.ext import PrettyPrint
  8. from Key import *
  9.  
  10. def run_script(script, sok):
  11.     a = __import__(script)
  12.     a.run(sok)
  13.  
  14. modifiers = {
  15.     'shift': 1,
  16.     'caps': 2,
  17.     'control': 4,
  18.     'mod1': 8,
  19.     'mod2': 16,
  20.     'mod3': 32,
  21.     'mod4': 64,
  22.     'mod5': 128 }
  23. modDic = {
  24.     'LWIN': ('Win', 64),
  25.     'RTSH': ('\xe2\x87\xa7'.decode('utf-8'), 1),
  26.     'LFSH': ('\xe2\x87\xa7'.decode('utf-8'), 1),
  27.     'RALT': ('Alt Gr', 128),
  28.     'LALT': ('Alt', 8),
  29.     'RCTL': ('Ctrl', 4),
  30.     'LCTL': ('Ctrl', 4),
  31.     'CAPS': ('CAPS', 2),
  32.     'NMLK': ('Nm\nLk', 16) }
  33. otherDic = {
  34.     'RWIN': 'Win',
  35.     'MENU': 'Menu',
  36.     'BKSP': '\xe2\x87\xa6'.decode('utf-8'),
  37.     'RTRN': 'Return',
  38.     'TAB': 'Tab',
  39.     'INS': 'Ins',
  40.     'HOME': 'Hm',
  41.     'PGUP': 'Pg\nUp',
  42.     'DELE': 'Del',
  43.     'END': 'End',
  44.     'PGDN': 'Pg\nDn',
  45.     'UP': '\xe2\x86\x91'.decode('utf-8'),
  46.     'DOWN': '\xe2\x86\x93'.decode('utf-8'),
  47.     'LEFT': '\xe2\x86\x90'.decode('utf-8'),
  48.     'RGHT': '\xe2\x86\x92'.decode('utf-8'),
  49.     'KP0': '0',
  50.     'KP1': '1',
  51.     'KP2': '2',
  52.     'KP3': '3',
  53.     'KP4': '4',
  54.     'KP5': '5',
  55.     'KP6': '6',
  56.     'KP7': '7',
  57.     'KP8': '8',
  58.     'KP9': '9',
  59.     'KPDL': 'Del',
  60.     'KPEN': 'Ent' }
  61. funcKeys = (('ESC', 65307), ('F1', 65470), ('F2', 65471), ('F3', 65472), ('F4', 65473), ('F5', 65474), ('F6', 65475), ('F7', 65476), ('F8', 65477), ('F9', 65478), ('F10', 65479), ('F11', 65480), ('F12', 65481), ('Prnt', 65377), ('Scroll', 65300), ('Pause', 65299))
  62. keysyms = {
  63.     'space': 65408,
  64.     'insert': 65438,
  65.     'home': 65360,
  66.     'page_up': 65365,
  67.     'page_down': 65366,
  68.     'end': 65367,
  69.     'delete': 65439,
  70.     'return': 65293,
  71.     'backspace': 65288 }
  72.  
  73. def create_default_layout_XML(name, vk, sok):
  74.     '''Reads layout stored within onBoard and outputs it to XML'''
  75.     doc = minidom.Document()
  76.     keyboard_element = doc.createElement('keyboard')
  77.     keyboard_element.setAttribute('id', name)
  78.     doc.appendChild(keyboard_element)
  79.     f = open(os.path.join(sok.SOK_INSTALL_DIR, 'layouts', 'template.svg'))
  80.     baseDoc = minidom.parse(f)
  81.     f.close()
  82.     paneDocs = []
  83.     for pane in sok.keyboard.panes:
  84.         paneDoc = deepcopy(baseDoc)
  85.         paneDocs.append((pane, paneDoc))
  86.     
  87.     pane = sok.keyboard.basePane
  88.     read_layout_from_sok(doc, baseDoc, pane, vk, name)
  89.     for paneDoc in paneDocs:
  90.         read_layout_from_sok(doc, paneDoc[1], paneDoc[0], vk, name)
  91.     
  92.     docFile = open(os.path.join(os.path.expanduser('~'), '.sok', 'layouts', '%s.sok' % name), 'w')
  93.     PrettyPrint(doc, docFile)
  94.     docFile.close()
  95.     docFile = open(os.path.join(os.path.expanduser('~'), '.sok', 'layouts', '%s-%s.svg' % (name, sok.keyboard.basePane.ident)), 'w')
  96.     PrettyPrint(baseDoc, docFile)
  97.     docFile.close()
  98.     for pane in paneDocs:
  99.         docFile = open(os.path.join(os.path.expanduser('~'), '.sok', 'layouts', '%s-%s.svg' % (name, pane[0].ident)), 'w')
  100.         PrettyPrint(pane[1], docFile)
  101.         docFile.close()
  102.     
  103.  
  104.  
  105. def read_layout_from_sok(doc, svgDoc, pane, vk, name):
  106.     basePane_element = make_xml_pane(doc, pane.ident, '%s-%s.svg' % (name, pane.ident), pane.rgba, pane.fontSize)
  107.     doc.documentElement.appendChild(basePane_element)
  108.     svgDoc.documentElement.setAttribute('width', str(pane.viewPortSizeX))
  109.     svgDoc.documentElement.setAttribute('height', str(pane.viewPortSizeY))
  110.     print 2
  111.     for keyKey, keyVal in pane.keys.items():
  112.         if keyVal.__class__ == RectKey:
  113.             svgDoc.documentElement.appendChild(make_xml_rect(doc, keyKey, keyVal.x, keyVal.y, keyVal.width, keyVal.height, keyVal.rgba))
  114.             labels = [
  115.                 '',
  116.                 '',
  117.                 '',
  118.                 '',
  119.                 '']
  120.             for n in range(len(keyVal.labels)):
  121.                 
  122.                 try:
  123.                     labels[n] = keyVal.labels[n].decode('utf-8')
  124.                 continue
  125.                 except UnicodeDecodeError:
  126.                     labels[n] = '?'
  127.                     continue
  128.                 
  129.  
  130.             
  131.             basePane_element.appendChild(make_xml_key(doc, keyKey, labels, keyVal.actions, keyVal.sticky, keyVal.fontOffsetX, keyVal.fontOffsetY))
  132.             continue
  133.         None<EXCEPTION MATCH>UnicodeDecodeError
  134.         if keyVal.__class__ == LineKey:
  135.             print 'funky keys not yet implemented'
  136.             continue
  137.     
  138.  
  139.  
  140. def make_xml_pane(doc, ident, filename, rgba, font):
  141.     pane_element = doc.createElement('pane')
  142.     pane_element.setAttribute('id', ident)
  143.     pane_element.setAttribute('filename', filename)
  144.     pane_element.setAttribute('backgroundRed', str(rgba[0]))
  145.     pane_element.setAttribute('backgroundGreen', str(rgba[1]))
  146.     pane_element.setAttribute('backgroundBlue', str(rgba[2]))
  147.     pane_element.setAttribute('backgroundAlpha', str(rgba[3]))
  148.     pane_element.setAttribute('font', str(font))
  149.     return pane_element
  150.  
  151.  
  152. def make_xml_rect(doc, ident, x, y, width, height, rgba):
  153.     rect_element = doc.createElement('rect')
  154.     rect_element.setAttribute('id', ident)
  155.     rect_element.setAttribute('x', str(x))
  156.     rect_element.setAttribute('y', str(y))
  157.     rect_element.setAttribute('width', str(width))
  158.     rect_element.setAttribute('height', str(height))
  159.     rect_element.setAttribute('style', 'fill:#%s%s%s;stroke:#000000;' % (dec_to_hex_colour(rgba[0]), dec_to_hex_colour(rgba[1]), dec_to_hex_colour(rgba[2])))
  160.     return rect_element
  161.  
  162.  
  163. def dec_to_hex_colour(dec):
  164.     hexString = hex(int(255 * dec))[2:]
  165.     if len(hexString) == 1:
  166.         hexString = '0' + hexString
  167.     
  168.     return hexString
  169.  
  170.  
  171. def make_xml_key(doc, ident, labels, actions, sticky, fontOffsetX, fontOffsetY):
  172.     key_element = doc.createElement('key')
  173.     if labels[0]:
  174.         key_element.setAttribute('label', labels[0])
  175.     
  176.     if labels[1]:
  177.         key_element.setAttribute('cap_label', labels[1])
  178.     
  179.     if labels[2]:
  180.         key_element.setAttribute('shift_label', labels[2])
  181.     
  182.     if labels[3]:
  183.         key_element.setAttribute('altgr_label', labels[3])
  184.     
  185.     if labels[4]:
  186.         key_element.setAttribute('altgrNshift_label', labels[4])
  187.     
  188.     key_element.setAttribute('id', ident)
  189.     if actions[0]:
  190.         key_element.setAttribute('char', actions[0])
  191.     elif actions[1]:
  192.         key_element.setAttribute('keysym', str(actions[1]))
  193.     elif actions[2]:
  194.         key_element.setAttribute('press', actions[2])
  195.     elif actions[3]:
  196.         for key, val in modifiers.items():
  197.             if actions[3] == val:
  198.                 key_element.setAttribute('modifier', key)
  199.                 continue
  200.         
  201.     elif actions[4]:
  202.         key_element.setAttribute('macro', actions[4])
  203.     elif actions[5]:
  204.         key_element.setAttribute('script', actions[5])
  205.     
  206.     if fontOffsetX:
  207.         key_element.setAttribute('font_offset_x', fontOffsetX)
  208.     
  209.     if fontOffsetY:
  210.         key_element.setAttribute('font_offset_y', fontOffsetY)
  211.     
  212.     if sticky:
  213.         key_element.setAttribute('sticky', 'true')
  214.     else:
  215.         key_element.setAttribute('sticky', 'false')
  216.     return key_element
  217.  
  218. if __name__ == '__main__':
  219.     from sys import argv
  220.     if argv[0]:
  221.         from virtkey import virtkey
  222.         from sok import Sok
  223.         s = Sok()
  224.         vk = virtkey()
  225.         create_default_layout_XML(argv[0], vk, s)
  226.     else:
  227.         print 'Type name for personalised layout'
  228.     s.clean
  229.  
  230.